<?php
namespace Controller;
use \GatewayWorker\Lib\Gateway;
use \GatewayWorker\Lib\DbConnection;
use \GatewayWorker\Lib\think\Db;
class LiveChat{
	/**
	 * @初始化业务逻辑
	 */
   public static $redis = "";
   public function __construct()
   {
	   $this->indexRedis();
   }
   public function indexRedis(){
	   self::$redis = new \redis();
	   self::$redis->connect('127.0.0.1', 6379);
   }
   //普通用户绑定UID并且进行分组绑定
   public function bindUidandUser($client_id, $data){
	    var_dump($data);
	   //UID和用户名绑定
	   $client_ids = Gateway::getClientIdByUid($data['user_name']);
	   foreach($client_ids as $key => $value){
		   Gateway::closeClient($value);
	   }
	   Gateway::bindUid($client_id,$data['user_name']);
	   $result["method_type"] ="bindUidandUser";
	   $result["Code"] = "200";
	   $result["Msg"] = "绑定成功";
	   sendToClient($client_id,$result);
   }
   //用户进入客服分组
   public function usertogroup($client_id, $data){
	   $result["method_type"] ="usertogroup";
	   if(self::$redis->exists($data["project_uniqueid"])){
		    $groups = json_decode(self::$redis->get($data["project_uniqueid"]));
			$groups[] = $data["user_name"];
			$groups = array_unique($groups);
		    $count = count($groups);
			$groups = json_encode($groups);
			self::$redis->set($data["project_uniqueid"],$groups);
			//用户可进行聊天状态
			self::$redis->set($data["user_name"]."liao","0");
			$result["Code"] = "200";
			$result["Msg"] = "正在等待客服接入，当前有超过".($count+1)."人正在排队!";
			sendToClient($client_id,$result);
			$result["Code"] = "200";
			$result["Msg"] = "用户列表!";
			$result["method_type"] ="getuserlists";
			$result["data"] = json_decode($groups);
			//sendToUid($data["to_user_name"],$result);
			Gateway::sendToGroup($data["project_uniqueid"],json_encode($result));
	   }else{
		   $result["Code"] = "300";
		   $result["Msg"] = "客服不在线!";
		   sendToClient($client_id,$result);
	   }
   }
   //发送消息
   public function sendMsgToUser($client_id, $data){
	   var_dump($data);
	   $result["method_type"] ="sendMsgToUser";
	   $result["Code"] = "200";
	   $result["Msg"] = "发送成功";
	   $result["from_user"] = $data["user_name"];
	   $result["to_user"] = $data["to_user_name"];
	   $result["content"] = "";
	   $result["picture"] = "";
	   $table_name = strtolower($data['project_uniqueid']);
	   if(Gateway::isUidOnline($data["to_user_name"])){
		   var_dump("当前状态:".self::$redis->get($data["user_name"]."liao"));
		   if((self::$redis->get($data["user_name"]."liao") == "0")){
			   if(self::$redis->exists($data["user_name"]."liao")){
				   $result["method_type"] ="sendMsgToUser";
			       $result["Code"] = "301";
				   $result["Msg"] = "请等待客服接入!";
				   sendToUid($data["user_name"],$result);
			   }else{
				   if(!empty($data["picture"])){
					   Db::name($table_name)->insert(array("from_user"=>$data["user_name"],"to_user"=>$data["to_user_name"],"add_time"=>time(),"picture"=>$data["picture"]));
					   $result["picture"] = "http://47.99.211.99/workerman-chat/imgs/".$data["picture"];
				   }else{
					   Db::name($table_name)->insert(array("from_user"=>$data["user_name"],"to_user"=>$data["to_user_name"],"add_time"=>time(),"content"=>$data["content"]));
					   $result["content"] = $data["content"];
				   }
				   sendToUid($data["to_user_name"],$result);
				   sendToClient($client_id,$result);
			   }
			   
		   }else{
				if(!empty($data["picture"])){
				   Db::name($table_name)->insert(array("from_user"=>$data["user_name"],"to_user"=>$data["to_user_name"],"add_time"=>time(),"picture"=>$data["picture"]));
				   $result["picture"] = "http://47.99.211.99/workerman-chat/imgs/".$data["picture"];
			   }else{
				   Db::name($table_name)->insert(array("from_user"=>$data["user_name"],"to_user"=>$data["to_user_name"],"add_time"=>time(),"content"=>$data["content"]));
				   $result["content"] = $data["content"];
			   }
			   sendToClient($client_id,$result);
			   sendToUid($data["to_user_name"],$result);
		   }
		    
			
	   }else{
		    $result["method_type"] ="sendMsgToUser";
		   $result["Code"] = "300";
		   $result["Msg"] = "对方已掉线!";
		   sendToUid($data["user_name"],$result);
	   }
   }
   //获取历史聊天记录接口
   public function gethistroyinfo($client_id, $data){
	   $page = $data["page"];
	   $limit = $data["pageSize"];
	   $current = ($page-1)*$limit;
	   //var_dump($current);
	   $where = "(from_user='".$data["user_name"]."' and to_user='".$data["to_user_name"]."') or (to_user='".$data["user_name"]."' and from_user='".$data["to_user_name"]."')";
	   $table_name = strtolower($data['project_uniqueid']);
	   $lists = Db::name($table_name)->where($where)->limit($current,$limit)->order("id desc")->select();
	   
	   foreach($lists as $key=>$value){
		   if(!empty($value['picture'])){
			  $lists[$key]['picture'] = "http://47.99.211.99/workerman-chat/imgs/".$value["picture"];
		   }
		   $lists[$key]['add_time']= date("Y/m/d H:i:s",$value["add_time"]);
	   }
	   $result["method_type"] ="gethistroyinfo";
	   $result["Code"] = "200";
	   $result["Msg"] = "获取记录成功!";
	   $result["data"] = $lists;
	   var_dump($result);
	   sendToClient($client_id,$result);
   }
   //后台客服登录后创建组
   public function creategruop($client_id, $data){
	   $result["method_type"] ="creategruop";
	   $result["Code"] = "200";
	   $result["Msg"] = "创建组成功";
	   Gateway::joinGroup($client_id,$data["project_uniqueid"]);
	   $count = abs(Gateway::getClientIdCountByGroup($data["project_uniqueid"]));
	   if($count<=1){
		   $groups = json_encode(array());
		   self::$redis->set($data["project_uniqueid"],$groups);
	   }
	   sendToClient($client_id,$result);
   }
   //管理员退出删除组
   public function deletegroup($client_id, $data){
	   $result["method_type"] ="deletegroup";
	   $result["Code"] = "200";
	   $result["Msg"] = "删除组成功";
	  
	   var_dump("断开用户:".$data["user_name"]);
	   self::$redis->del($data["user_name"]."liao");
	   //客服下线
	   Gateway::leaveGroup($client_id,$data["project_uniqueid"]);
	   $count = Gateway::getClientIdCountByGroup($data["project_uniqueid"]);
	   if($count<=0){
		   self::$redis->del($data["project_uniqueid"]);
		   Gateway::ungroup($data["project_uniqueid"]);
	   }
	   
	   if(self::$redis->exists($data["to_user_name"]."liao")){
		  self::$redis->set($data["to_user_name"]."liao","0");
	   }
	   sendToClient($client_id,$result);
   }
   //客服接入用户
    public function bussinstocutsom($client_id, $data){
		$result["method_type"] ="bussinstocutsom";
		$user_name = Gateway::getUidByClientId($client_id);
		//获取用户池列表
		$groups = json_decode(self::$redis->get($data["project_uniqueid"]),true);
		$key = (array_search($data["to_user_name"],$groups));
		unset($groups[$key]);
		$groups = json_encode($groups);
		self::$redis->set($data["project_uniqueid"],$groups);
		if(Gateway::isUidOnline($data["to_user_name"])){
				$result["Code"] = "200";
				$result["Msg"] = "接入用户成功";
				$result["to_user_name"] = $data["user_name"];
				self::$redis->set($data["to_user_name"]."liao","1");
				sendToUid($data["to_user_name"],$result);
				
		}else{
				$result["Code"] = "300";
				$result["Msg"] = "用户已掉线,请接入下一位用户!";
				self::$redis->del($data["to_user_name"]."liao");
		}
	    sendToClient($client_id,$result);
		$this->getuserlistsgroup($client_id, $data);
		//Gateway::sendToGroup($data["project_uniqueid"],json_encode($result));
	}
	//客服组获取用户等待数据
	 public function getuserlistsgroup($client_id, $data){
		  $user_name = Gateway::getUidByClientId($client_id);
		  $groups = json_decode(self::$redis->get($data["project_uniqueid"]),true);
		  $result["Code"] = "200";
		  $result["Msg"] = "请求成功!";
		  $result["method_type"] ="getuserlists";
		  $result["data"] = $groups;
		//  sendToClient($client_id,$result);
		Gateway::sendToGroup($data["project_uniqueid"],json_encode($result));
	 }
	//客服获取用户等待数据
	 public function getuserlists($client_id, $data){
		  $user_name = Gateway::getUidByClientId($client_id);
		  $groups = json_decode(self::$redis->get($data["project_uniqueid"]),true);
		  $result["Code"] = "200";
		  $result["Msg"] = "请求成功!";
		  $result["method_type"] ="getuserlists";
		  $result["data"] = $groups;
		  sendToClient($client_id,$result);
	 }
	 //结束当前用户聊天接入下一位
	 public function downnowuser($client_id, $data){
		  $result["Code"] = "200";
		  $result["Msg"] = "接入成功!";
		  $result["method_type"] ="downnowuser";
		  self::$redis->set($data["user_name"]."liao","0");
		  sendToClient($client_id,$result);
	 }
	 //后台客服登录接口
	 public function adminlogin($client_id, $data){
		 $count = Db::name("kefulist")->where("kefu_name",$data["kefu_name"])->where("pj_uniqid",$data["project_uniqueid"])->count();
		 if($count>0){
			 $admin_info = Db::name("kefulist")->where("kefu_name",$data["kefu_name"])->where("pj_uniqid",$data["project_uniqueid"])->find();
			 $repassword = md5($data["password"]);
			 if($admin_info["password"] == $repassword){
				 $result["Code"] = "200";
				  $result["Msg"] = "登录成功!";
				  $result["method_type"] ="adminlogin";
				  $result["data"] = $groups;
				  sendToClient($client_id,$result); 
			 }else{
				  $result["Code"] = "300";
				  $result["Msg"] = "登录密码错误!";
				  $result["method_type"] ="adminlogin";
				  $result["data"] = $groups;
				  sendToClient($client_id,$result); 
			 }
		 }else{
			  $result["Code"] = "300";
			  $result["Msg"] = "客服管理员不存在!";
			  $result["method_type"] ="adminlogin";
			  $result["data"] = $groups;
			  sendToClient($client_id,$result);
		 }
	 }
}